//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package alp.starcode.sanping.user.service;

import alp.starcode.common.base.exception.ValidatedException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import alp.starcode.common.util.encrypt.MD5EncodeUtils;
import alp.starcode.sanping.framework.base.BaseService;
import alp.starcode.sanping.framework.database.mariadb.mybatis.dao.UserDao;
import alp.starcode.sanping.framework.database.mariadb.mybatis.entity.User;
import cn.hutool.core.util.ObjectUtil;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class LoginService extends BaseService {

    @Autowired
    UserDao userDao;

    private static final Logger log = LoggerFactory.getLogger(LoginService.class);

    public User login(String loginName, String loginPwd, String captcha, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isEmpty(loginName)) {
            throw new ValidatedException("用户名不能为空");
        } else if (StringUtils.isEmpty(loginPwd)) {
            throw new ValidatedException("用户密码不能为空");
        } else {
            MPJLambdaWrapper<User> queryWrapper = new MPJLambdaWrapper<>();
            queryWrapper.select(User::getLoginAccount, User::getUserName);
            queryWrapper.eq(User::getLoginAccount, loginName);
            queryWrapper.eq(User::getLoginPassword, MD5EncodeUtils.MD5Encode(loginPwd));
            // 有效数据
            queryWrapper.eq(User::getIsDeleted, Boolean.FALSE);
            User user = userDao.getOne(queryWrapper);
            if(ObjectUtil.isEmpty(user)) {
                throw new ValidatedException("用户名或密码错误");
            }
            return user;
        }
    }

}
